/*
 * 系统参数窗口
 * 舞者无罪
 * 2014.11.24
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "com.h"
#include "tscal.h"

/*
 * below is written by myself in minigui
 */
void EnableTscal(int enable);
void GetTscalData(int *xy);
void SetTscalData(int xy[][2], int fb[][2]);
/*
 * end minigui
 */

/*
 *
 */
static int TscalWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    int fb[5][2] = {
            {50, 50},
            {750, 50},
            {750, 550},
            {50, 550},
            {400, 300}
    };
    switch(message)
    {
    case MSG_CREATE:
        {
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);

            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));

            rect.left += 300;
            rect.right -= 300;
            rect.top = 200;
            rect.bottom = rect.top + 30;
            DrawText(hdc, GetWindowCaption(hWnd), -1, &rect, DT_CENTER
                    | DT_VCENTER | DT_SINGLELINE);

            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            rect.left = fb[data.pwnd_tscal->index][0] - 10;
            rect.right = rect.left + 20;
            rect.top = fb[data.pwnd_tscal->index][1] - 10;
            rect.bottom = rect.top + 20;
            DrawCross(hdc, &rect);

            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
//            EnableTscal(FALSE);
            ShowCursor(SW_SHOW);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls (hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            if(lParam)
            {
                memcpy(&rect, (RECT*) lParam, sizeof(RECT));
            }
            else
            {
                GetClientRect(hWnd, &rect);
            }
            int width = GET_RECT_WIDTH(rect);
            int height = GET_RECT_HEIGHT(rect);
            SetBrushColor(hdc, COLOR_HQ_BLACK(hdc));
            FillBox(hdc, rect.left, rect.top, width, height);
            ReleaseDC(hdc);
        }
        return 0;
    case MSG_LBUTTONDOWN:
        {
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
//            GetTscalData(data.pwnd_tscal->data[data.pwnd_tscal->index]);
            if(data.pwnd_tscal->index > 3)
            {
//                SetTscalData(data.pwnd_tscal->data, fb);
                PostMessage(hWnd, MSG_CLOSE, 0, 0);
            }
            else
            {
                RECT rect;
                rect.left = fb[data.pwnd_tscal->index][0] - 10;
                rect.right = rect.left + 20;
                rect.top = fb[data.pwnd_tscal->index][1] - 10;
                rect.bottom = rect.top + 20;
                InvalidateRect(hWnd, &rect, TRUE);
                ++data.pwnd_tscal->index;
                rect.left = fb[data.pwnd_tscal->index][0] - 10;
                rect.right = rect.left + 20;
                rect.top = fb[data.pwnd_tscal->index][1] - 10;
                rect.bottom = rect.top + 20;
                InvalidateRect(hWnd, &rect, TRUE);
            }
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateTscalWindow(HWND hHosting)
{
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(480);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = TscalWinProc;
    CreateInfo.lx = 0;
    CreateInfo.ty = 0;
    CreateInfo.rx = CX_SCREEN;
    CreateInfo.by = CY_SCREEN;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = (int)calloc(1, sizeof(WND_TSCAL));
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        ShowCursor(SW_HIDE);
//        EnableTscal(TRUE);
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
